/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.dsw2.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.dsw2.biblioteca8a.bl.CategoriaBlLocal;
import mx.itschapala.dsw2.biblioteca8a.modelo.Categoria;

/**
 *
 * @author Maq.9
 */
@ManagedBean
@SessionScoped
public class CategoryBean {
    @EJB
    private CategoriaBlLocal categoriaBl;

  //------------variables----------------
  private List<Categoria> lista;
  private Categoria category;
  private String accion;
//-------------constructor-------------
    public CategoryBean() {
        category= new Categoria();
    }
    
   
//--------------get y set--------------

    public List<Categoria> getLista() {
        lista=categoriaBl.getAll();
        System.out.println(lista);
        
        return lista;
    }
     public Categoria getCategory() {
        return category;
    }

    public void setCategory(Categoria category) {
        this.category = category;
 
    }
    //-------------acciones-----------------
public String procesarAccion(){
    if(accion.equalsIgnoreCase("Nuevo")){
    categoriaBl.register(category);
    }else{
    if(accion.equalsIgnoreCase("Editar")){
    categoriaBl.ModiFy(category);
    }else{
    if(accion.equalsIgnoreCase("Eliminar")){
        categoriaBl.delete(category);
        }
    }
    }
    accion="";
    return "CategoriaLista";

}    
 
//-------------listeners----------------
public void prepararNuevo(ActionEvent e){
    category= new Categoria(); 
    accion="Nuevo";
}
public void prepararEditar(ActionEvent evt){
       System.out.println(evt.getComponent().getClientId());
       int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
       System.out.println("el numero es:"+ index);
       category=lista.get(index);
       accion="Editar";               
}
public void prepararEliminar(ActionEvent ev){
    int index = Integer.parseInt(ev.getComponent().getClientId().split(":")[2]);      
       category=lista.get(index);
accion="Eliminar";
}
}
